unit ThreadProcedure;

interface

uses
  Classes, Sysutils,
  CustomThreadBase;

Type
  TThreadProc = procedure of Object;
  TThreadProcedure = Class(TThread)
    private
      FProcedure: TThreadProc;
    protected
      procedure Execute; override;
    public
      ProcedureID:String;
      success: Boolean;
      Constructor Create(aProcedure:TThreadProc; OnTerminate: TNotifyEvent; aProcedureID:String=''); reintroduce;
  End;

  TThreadProcedureEx = class(TThread)
    private
      FStop: Boolean;
      FProcedure, FProcedureOnce: TThreadProc;
    protected
      Procedure Execute; override;
    public
      Constructor Create(aProcedureOnece, aProcedure:TThreadProc; OnTerminate: TNotifyEvent); reintroduce;
      property Stop: Boolean read FStop Write FStop;
  end;

implementation

uses
  CodeSiteLogging;


constructor TThreadProcedure.Create(aProcedure: TThreadProc; OnTerminate: TNotifyEvent; aProcedureID:String='');
begin
  inherited Create(True);
  Self.FreeOnTerminate := True;
  Self.OnTerminate := OnTerminate;
  FProcedure := aProcedure;
  Success := false;
  ProcedureID := aProcedureID;
  Resume;
end;

procedure TThreadProcedure.Execute;
begin
  FProcedure;
end;

{ TThreadFuncEx }

constructor TThreadProcedureEx.Create(aProcedureOnece, aProcedure: TThreadProc; OnTerminate: TNotifyEvent);
begin
  inherited Create(True);
  Self.FreeOnTerminate := True;
  Self.OnTerminate := OnTerminate;
  FProcedure := aProcedure;
  FProcedureOnce := aProcedureOnece;
  FStop := False;
//  self.Priority := tpHighest;
  Resume;
end;

procedure TThreadProcedureEx.Execute;
begin
  if Assigned(FProcedureOnce) then
    FProcedureOnce;

  while(not Terminated) and (not FStop) do
  begin
    if fStop then break;
    FProcedure;
    if fStop then break;
    Sleep(10);
    if fStop then break;
  end;
end;

end.
